package tw.org.iii.example.location;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class ActMain extends Activity implements LocationListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actmain);
		
		TextView lblLocation = (TextView) findViewById(R.id.lblLocation);
		//取得目前的Location管理，取得服務
		LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);

		if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)
				|| manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
			//可以定位GPS，或者NetWork
			Location location = manager.getLastKnownLocation("network");

			if (location != null) {
				StringBuffer l_strBuff = new StringBuffer();
				//22.639798
				//120.302092
				l_strBuff.append("位置：緯度:");
				l_strBuff.append(Double.toString(location.getLongitude()));
				l_strBuff.append(",經度：");
				l_strBuff.append(Double.toString(location.getLatitude()));
				lblLocation.setText(l_strBuff.toString());
			} else
				lblLocation.setText("定位失敗");
		} else{
			lblLocation.setText("尚未開啟定位功能");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.act_main, menu);
		return true;
	}

	@Override
	public void onLocationChanged(Location location) {

	}

	@Override
	public void onProviderDisabled(String provider) {

	}

	@Override
	public void onProviderEnabled(String provider) {

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

}
